﻿using LaserTag.Infrastructure;
using LaserTag.Infrastructure.Events;
using LaserTag.Module.MediaKit.ViewModels;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace LaserTag.Module.MediaKit.Controllers
{
    public class MediaKitController : IMediaKitController
    {
        private readonly IRegionManager m_regionManager;
        private readonly IVideoCaptureViewModel m_videoCaptureViewModel;
        private IEventAggregator m_eventAggregator;

        public MediaKitController(IRegionManager regionManager, 
                                  IVideoCaptureViewModel videoCaptureViewModel, 
                                  IConfigurationSource configurationSource, 
                                  IEventAggregator eventAggregator)
        {
            m_eventAggregator = eventAggregator;
            m_videoCaptureViewModel = videoCaptureViewModel;
            m_regionManager = regionManager;

            eventAggregator.GetEvent<SetupCategorySelected>().Subscribe(OnSetupCategorySelected, true);
        }

        private void OnSetupCategorySelected(SetupCategory category)
        {
            if (category != SetupCategory.CameraSetup)
                return;

            m_regionManager.Regions[RegionNames.SetupRegion].Activate(m_videoCaptureViewModel.View);
        }


        private object GetCaptureView()
        {
            return m_videoCaptureViewModel.View;
        }

        public void Run()
        {
            m_regionManager.RegisterViewWithRegion(RegionNames.SetupRegion, GetCaptureView);
        }

        public void Shutdown()
        {
        }
    }
}
